#include "main.h"
#include "list_drawing.h"

CList_drawing::CList_drawing()
{
	for(int i=0;i<15;i++)
	{
		for_draw[i]=NULL;
	}
	for(i=0;i<15;i++)
	{
		all_cub[i]=&cubiki[i];
	}
}

int CList_drawing::set_data(CCub_for_world* P_pcfw,CVector3 P_stor,CCubic* P_pcub)
{
	for(int i=0;i<15;i++)
	{
		if(P_pcub==all_cub[i])
		{
			
				spisok_cub* test=new spisok_cub;
				test->kor=P_pcfw;
				test->stor=P_stor;
				test->next=for_draw[i];
				for_draw[i]=test;
				return true;
		}
	}
	return false;
}

void CList_drawing::draw()
{
	for(int i=0;i<15;i++)
	{
			if(tek_id!=all_cub[i]->returnidtext())
			{
				glBindTexture(GL_TEXTURE_2D, tek_id=all_cub[i]->returnidtext());
			}
			spisok_cub* test=for_draw[i];
			while(test)
			{
				all_cub[i]->Draw(test->kor->return_pos(),1,test->stor);
				test=test->next;
			}
	}
}

void CList_drawing::delete_one_cub(spisok_cub* cub_for_del,int i,bool sled)
{
	if(sled)
	{
		spisok_cub* test=cub_for_del->next;
		cub_for_del->next=test->next;
		delete test;
	}
	else
	{
		for_draw[i]=for_draw[i]->next;
		delete cub_for_del;
	}
}

int CList_drawing::delete_infa_cub(CCub_for_world* P_pcfw,CVector3 P_stor,CCubic* P_pcub)
{
	for(int i=0;i<15;i++)
	{
		if(P_pcub==all_cub[i]&&for_draw[i])
		{
			if(for_draw[i]->kor==P_pcfw&&for_draw[i]->stor.x==P_stor.x&&for_draw[i]->stor.z==P_stor.z)
			{
				this->delete_one_cub(for_draw[i],i,false);
				return true;
			}
			else
			{
				spisok_cub* test=for_draw[i];
				while(test->next)
				{
					bool sled=true;
					if(test->next->kor==P_pcfw&&test->next->stor.x==P_stor.x&&test->next->stor.z==P_stor.z)
					{
						this->delete_one_cub(test,i,true);
						return true;
					}
					test=test->next;
				}
			}
		}
	}
	return false;
}

CList_drawing::~CList_drawing()
{
	for(int i=0;i<15;i++)
	{
		spisok_cub* test=for_draw[i];
		while(test)
		{
			test=for_draw[i]->next;
			delete for_draw[i];
			for_draw[i]=test;
		}
	}
}